import time

import xlrd
import xlwt
# import openpyxl
# import xlutils.copy
# rd = xlrd.open_workbook("D:\pythonProject\excel\log数据.xlsx", formatting_info = True)   # 打开文件
# wt = xlutils.copy.copy(rd)
wb = xlwt.Workbook(encoding="utf-8")
'''
sheet_names = wb.sheetnames
if "日志统计" not in sheet_names:
    sheet = wb.add_sheet("日志统计")
else:
    sheet = wt.sheet_by_name("日志统计")
'''
# sheet = wb.add_sheet("日志统计")
sheet = wb.sheet_by_name("日志统计")
head_data = ['错误出现次数', '调试信息出现次数', '警告出现了次数', '普通消息出现次数']
li = 1
# 账户转账数据
for head in head_data:
    sheet.write(0, head_data.index(head), head)
while True:

    time.sleep(2)
    f = open(file=r"C:\Users\xmy\Desktop\b.log", mode="r", encoding="utf-8")
    data = f.readlines()  # 读取所有行,并将每一行数据放入列表中
    f.close()
    I = 0
    W = 0
    E = 0
    D = 0
    if len(data) != 0:  # 如果数据不为空
        for i in data:
            data_list = i.split(":")[0]
            # print("data_list", data_list)
            if len(data_list) == 0:
                continue
            else:
                k = data_list[0]
                # print("key", key)
                if k == "I":
                    I = I + 1
                elif k == "W":
                    W = W + 1
                elif k == "E":
                    E = E + 1
                elif k == "D":
                    D = D + 1
                else:
                    continue

        print("错误出现了%d次，调试信息出现%d次，警告出现了%d次，普通消息出现%d次" % (I, W, E, D))
        sheet.write(li, 0, I)
        sheet.write(li, 1, W)
        sheet.write(li, 2, E)
        sheet.write(li, 3, D)
        li = li+1
        wb.save('D:\pythonProject\excel\log数据.xlsx')
    else:
        continue
